import { StorageKey, EventKey } from "../common/Key";

export const AudioMgr = {
    isMusicMute: false,
    isEffectMute: false,
    isVibrateMute: false,

    init() {
        this.isMusicMute = G.data.getItem(StorageKey.IS_MUSIC_MUTE, false);
        this.isEffectMute = G.data.getItem(StorageKey.IS_EFFECT_MUTE, false);
        this.isVibrateMute = G.data.getItem(StorageKey.IS_VIBRATE_MUTE, false);
    },

    changeMusicMuteState(autoPlay = false) {
        this.isMusicMute = !this.isMusicMute;
        G.data.setItem(StorageKey.IS_MUSIC_MUTE, this.isMusicMute);

        if (this.isMusicMute) {
            this.stopBgm();
        } else {
            if (!autoPlay) {
                return;
            }
            this.playBgm();
        }
    },
    changeEffectMuteState() {
        this.isEffectMute = !this.isEffectMute;
        G.data.setItem(StorageKey.IS_EFFECT_MUTE, this.isEffectMute);
    },
    changeVibrateMuteState() {
        this.isVibrateMute = !this.isVibrateMute;
        G.data.setItem(StorageKey.IS_VIBRATE_MUTE, this.isVibrateMute);
    },
    getIsMusicMute() {
        return this.isMusicMute;
    },
    getIsEffectMute() {
        return this.isEffectMute;
    },
    getIsVibrateMute() {
        return this.isVibrateMute;
    },

    playBgm(audioClip) {
        if (!audioClip) {
            if (!this.bgmClip) {
                return;
            }
        } else {
            this.bgmClip = audioClip;
        }
        if (this.isMusicMute) {
            return;
        }
        this.bgmId = cc.audioEngine.play(this.bgmClip, true)
    },

    stopBgm() {
        if (!this.bgmId && this.bgmId != 0) {
            return;
        }
        cc.audioEngine.stop(this.bgmId);
    },

    clearBgm(){
        this.bgmId = null;
        this.bgmClip = null;
    },

    playEffect(audioClip) {
        if (this.isEffectMute) {
            return;
        }
        cc.audioEngine.play(audioClip, false)
    },

}